package com.qf.weektest.websocket;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.weektest.vo.ResultVo;
import com.qf.weektest.weektest.entity.Tel;
import com.qf.weektest.weektest.service.IPollinfService;
import com.qf.weektest.weektest.service.ITelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

@Component
@ServerEndpoint("/webSocket")
@Slf4j
public class WebSocketServer {

    public static CopyOnWriteArraySet<Session> sessions = new CopyOnWriteArraySet<>();

    @Autowired
   static ITelService telService;



    @OnOpen
    public  void onOpen(Session session) throws IOException {

       sessions.add(session);


    }

    public  void sendMsg(String msg){
        try {
            for (Session session : sessions) {

                session.getBasicRemote().sendText(msg);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @OnClose
    public void onclose(Session session){

        sessions.remove(session);


    }




}
